﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.CommonSenseControls;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;

namespace SenseTimeTracker.Export
{
	public partial class ExportForm : CommonSenseForm
	{
		private SensePanelComboItem exportTypeComboBoxItem;
		private SensePanelComboItem exportPeriodComboBoxItem;
		private SensePanelDateTimeItemEx exportDateItem;

		/// <summary>
		/// Available export types
		/// </summary>
		public enum ExportType
		{
			/// <summary>
			/// Export to XML
			/// </summary>
			[Description("ExportTypeXML")]
			XML,

			/// <summary>
			/// Export to CSV
			/// </summary>
			[Description("ExportTypeCSV")]
			CSV,

			/// <summary>
			/// Export to TERP
			/// </summary>
			[Description("ExportTypeTERP")]
			TERP,

			/// <summary>
			/// Export to MyTyme
			/// </summary>
			[Description("ExportTypeMyTime")]
			MyTime,
		}

		/// <summary>
		/// Available export periods
		/// </summary>
		public enum ExportPeriod
		{
			/// <summary>
			/// Export whole week
			/// </summary>
			[Description("ExportPeriodWeek")]
			Week,

			/// <summary>
			/// Export whole month
			/// </summary>
			[Description("ExportPeriodMonth")]
			Month,
		}

		public ExportForm()
		{
			InitializeComponent();

			menuItemExport.Text = Resources.MenuItemExport;
			menuItemCancel.Text = Resources.MenuItemCancel;

			exportTypeComboBoxItem = new SensePanelComboItem();
			exportTypeComboBoxItem.LabelText = string.Format("{0}:", Resources.ExportToTabel);
			exportTypeComboBoxItem.ShowSeparator = true;
			foreach (KeyValuePair<object, string> exportType in EnumExtension.GetValues(Persistance.Instance.ExportType))
			{
				exportTypeComboBoxItem.Items.Add(new SensePanelComboItem.Item(exportType.Value, exportType.Key));
			}

			exportTypeComboBoxItem.SelectedValue = Persistance.Instance.ExportType;

			exportPeriodComboBoxItem = new SensePanelComboItem();
			exportPeriodComboBoxItem.LabelText = string.Format("{0}:", Resources.PeriodLabel);
			exportPeriodComboBoxItem.ShowSeparator = true;
			foreach (KeyValuePair<object, string> exportPeriod in EnumExtension.GetValues(Persistance.Instance.ExportPeriod))
			{
				exportPeriodComboBoxItem.Items.Add(new SensePanelComboItem.Item(exportPeriod.Value, exportPeriod.Key));
			}

			exportPeriodComboBoxItem.SelectedValue = Persistance.Instance.ExportPeriod;

			exportDateItem = new SensePanelDateTimeItemEx();
			exportDateItem.LabelText = string.Format("{0}:", Resources.ExportDate);
			exportDateItem.DateTime = DateTime.Now;
			exportDateItem.ShowSeparator = true;
			exportDateItem.DisplayStyle = SensePanelDateTimeItem.Style.Date;

			senseHeaderCtrl.Text = Resources.PanelExportName;
		}

		protected override void OnLoad(EventArgs e)
		{
			Bind();
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{
				Persistance.Instance.ExportType = (ExportType)exportTypeComboBoxItem.SelectedValue;
				Persistance.Instance.ExportPeriod = (ExportPeriod)exportPeriodComboBoxItem.SelectedValue;

				switch (Persistance.Instance.ExportType)
				{
					case ExportType.TERP:
						ExportTieto(exportDateItem.DateTime, Persistance.Instance.ExportPeriod, TietoExportHeaderType.TERP);
						break;
					case ExportType.MyTime:
						ExportTieto(exportDateItem.DateTime, Persistance.Instance.ExportPeriod, TietoExportHeaderType.MyTime);
						break;
					case ExportType.XML:
						ExportToXML(exportDateItem.DateTime, Persistance.Instance.ExportPeriod);
						break;
					case ExportType.CSV:
						ExportToCSV(exportDateItem.DateTime, Persistance.Instance.ExportPeriod);
						break;
					default:
						throw new NotSupportedException("Unsupported export type");
				}
			}
		}

		private void Bind()
		{
			senseListCtrl.TopIndex = 0;
			senseListCtrl.Clear();
			senseListCtrl.AddItem(new SensePanelDividerItem(string.Empty, Resources.ExportExplanationLabel));
			senseListCtrl.AddItem(exportTypeComboBoxItem);
			senseListCtrl.AddItem(exportPeriodComboBoxItem);
			senseListCtrl.AddItem(exportDateItem);
		}

		private void menuItemExport_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
		}

		private void menuItemCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}

		public IEnumerable<string> GetNames(Enum enumeration)
		{
			List<string> enumerations = new List<string>();
			foreach (FieldInfo fieldInfo in enumeration.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
			{
				enumerations.Add(fieldInfo.Name);
			}
			return enumerations;
		}

		/// <summary>
		/// This method calculates the calender week according to a given date
		/// </summary>
		/// <param name="date">The date where the calender week is within</param>
		/// <returns>the number of the calender week</returns>
		private int CalenderWeek(DateTime date)
		{
			DateTime time = date.AddDays((3 - ((int)(date.DayOfWeek + 7) % (int)(DayOfWeek.Saturday | DayOfWeek.Monday)))).Date;
			DateTime time2 = new DateTime(date.Year, 1, 4);
			DateTime time3 = time2.AddDays((3 - ((time2.Day + 7) % 7))).Date;
			TimeSpan span = time - time3;
			return (int)Math.Floor(1.5 + (span.TotalDays / 7.0));
		}
	}
}

